/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.ide.commons.search;

import com.rw.magallanes.core.Item;
import com.rw.magallanes.ide.main.MainFrame;
import com.rw.magallanes.ide.main.MainSystem;
import com.rw.magallanes.utilities.MessageType;
import com.rw.magallanes.utilities.Utilities;
import com.rw.table.ATableAction;
import com.rw.table.ATableModel;
import com.rw.table.editor.ICellEditor;
import com.rw.table.editor.TextCellEditor;
import com.rw.table.formatter.DoubleCellFormatter;
import com.rw.table.formatter.ICellFormatter;
import com.rw.table.formatter.StringCellFormatter;
import com.rw.table.render.ICellRenderer;
import com.rw.table.render.LabelCellRenderer;
import java.util.List;

/**
 *
 * @author Gonzalez
 */
public class SearchTableModel extends ATableModel implements SearchConstants {

    private List<Item> items;
    //
    private LabelCellRenderer labelRenderer = new LabelCellRenderer();
    private StringCellFormatter strFormatter = new StringCellFormatter();
    private DoubleCellFormatter dblFormatter = new DoubleCellFormatter(2);
    private TextCellEditor textEditor = new TextCellEditor();

    public SearchTableModel() {
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    @Override
    public ICellRenderer getRenderer(int row, int column) {
        return labelRenderer;
    }

    @Override
    public ICellEditor getEditor(int row, int column) {
        switch (column) {
            case COL_NAME:
                return textEditor;
            case COL_CATEGORY:
                return null;
            case COL_PRICE:
                return textEditor;
            default:
                throw new RuntimeException("Columna desconocida: " + column);
        }
    }

    @Override
    public ICellFormatter getFormatter(int row, int column) {
        switch (column) {
            case COL_NAME:
                return strFormatter;
            case COL_CATEGORY:
                return strFormatter;
            case COL_PRICE:
                return dblFormatter;
            default:
                throw new RuntimeException("Columna desconocida: " + column);
        }
    }

    @Override
    public ATableAction getEditableByClickAction(int row, int col) {
        return null;
    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        if (value == null) {
            return;
        }
        MainFrame mf = MainSystem.getInstance().getMainFrame();
        Item item = items.get(rowIndex);
        switch (columnIndex) {
            case COL_NAME:
                String strValue = (String) value;
                if (strValue != null && !strValue.isEmpty()) {
                    item.setName(strValue);
                } else {
                    Utilities.showMsg(mf, "Nombre invalido", MessageType.WARN);
                }
                break;
            case COL_PRICE:
                double dblValue = ((Double) value).doubleValue();
                if (dblValue >= 0) {
                    item.setPrice(dblValue);
                } else {
                    Utilities.showMsg(mf, "Precio invalido", MessageType.WARN);
                }
                break;
        }
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getRowCount() {
        if (items != null) {
            return items.size();
        }
        return 0;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Item item = items.get(rowIndex);
        switch (columnIndex) {
            case COL_NAME:
                return item.getName();
            case COL_CATEGORY:
                return item.getCategory();
            case COL_PRICE:
                return item.getPrice();
            default:
                throw new RuntimeException("Columna desconocida: " + columnIndex);
        }
    }
}
